<?php
/**
 * cron获取更新状态
 * @code-encode utf-8
 * @data-encode utf-8
 */


class mod_cron_engine
{
	var $current_process_id = 0;
	var $start_id = 0;
	var $cron_type ="";
	protected  function __construct($current_process_id,$cron_type="")
	{
		$this->current_process_id = $current_process_id;
		$this->cron_type = $cron_type ? $cron_type :  basename($_SERVER["PHP_SELF"]);
		
	}
	
	
	//获取开始id
	/*
	 * cron_type 			string 	cron类别，主要不要重复！
	 * current_process_id	int 	当前程序进程id
	 */
	function get_start_id()
	{
		$cron_type = $this->cron_type;
		$db_cron_start_id = new db_cron_start_id();
		$res = $db_cron_start_id->cron_get_item_by_type($cron_type,$this->current_process_id);
		$start_id = $res["start_id"];
		return $start_id;
	}
	
	//把当前进程开始ID初始化为0
	function ini_start_id()
	{
		$db_cron_start_id = new db_cron_start_id();
		$res = $this->replace_into_data($this->current_process_id,0);	
		return $res;
	}
	
	//修改当前进程start_id
	function replace_into_data($start_id)
	{
		global $done_replace;
		$done_replace = true;
		$cron_type = $this->cron_type;
		$db_cron_start_id = new db_cron_start_id();
		$res = $db_cron_start_id->cron_replace_into_data($cron_type,$start_id,$this->current_process_id);	
		
		return $res;
	}
	
	
	function before_run()
	{
		//获取当前类型处理的start_id
		$start_id = $this->get_start_id($this->current_process_id);
		$this->start_id = $start_id;
		
	}
	//获取cron详细信息
	function get_cron_engine_item()
	{
		$cron_type = $this->cron_type;
		$db_cron_start_id = new db_cron_start_id();
		$res = $db_cron_start_id->cron_get_item_by_type($cron_type,$this->current_process_id);
		return $res;
	}
	
	
	//最后执行如果整个进程都没有执行过replace,判断为这个进程已经执行到头，初始化
	function after_run()
	{
		global $done_replace;
		if($done_replace != true)
		{
			$this->ini_start_id();
		}
	}

}